var onLoad = function() {
    require(["html-header"]);
    require(["css!../css/login.css"]);
    require(["pointBackground", "css!../css/debug.css"]);
    setTimeout(() => $("#changeLineDisplay").trigger("click"), 200);
    $("#register").on("click", () => {

    })
    let name = $("#userNameFrame>input");
    let password = $("#passwordFrame>input");
    name.on("input", () => {
        name.val(name.val().replace(/[^A-Za-z0-9_]/, ""));
    })
    password.on("input", () => {
        password.val(password.val().replace(/[^A-Za-z0-9\.,_+=#$@\-]/, ""));
    })
    $("#login").on("click", function() {
        let nametag = $("#userNameFrame>span");
        let passwordtag = $("#passwordFrame>span");
        if (name.val() == "") {
            popperMessage(nametag, "用户名不能为空");
            return;
        }
        if(password.val() == ""){
            popperMessage(passwordtag, "密码不能为空");
            return;
        }
        $.ajax({
            url: "user/login",
            type: "POST",
            data: {
                "username": name.val(),
                "password": password.val()
            },
            success: function(data){
                window.location = "./";
            },
            error: function(xml){
                popperMessage($("#login"), xml.responseText);
            }
        })
    });

    function popperMessage(element, message, position = "left") {
        if (element.attr("aria-describedby") != undefined) element.popover('dispose');
        new bs.Popover(element[0], {
            content: message,
            delay: {
                "show": 250,
                "hide": 250
            },
            tigger: "hover",
            placement: position,
        });
        element.popover('show');
        let id = element.attr("aria-describedby");
        setTimeout(() => {
            if (element.attr("aria-describedby") != null && element.attr("aria-describedby") != id) return;
            element.popover('hide');
            setTimeout(() => {
                if (element.attr("aria-describedby") != null && element.attr("aria-describedby") != id) return;
                element.popover('dispose');
            }, 500);
        }, 2000);
    }
}
